package com.foton.controller;


import com.foton.designmodel.decorator.impl.BasePriceSum;
import com.foton.designmodel.decorator.impl.DecoratorMoneySum;
import com.foton.designmodel.decorator.impl.FullMoneySum;
import com.foton.designmodel.decorator.impl.VipPriceSum;
import com.foton.designmodel.decorator.orderprice.MoneySum;
import com.foton.designmodel.decorator.orderprice.entity.Order;
import com.foton.designmodel.proxy.SystemFileProxy;
import com.foton.designmodel.share.session.UserThreadLocal;
import com.foton.order.config.OrderMintor;
import com.foton.order.dto.OrderDto;
import com.foton.order.entity.OrderBase;
import com.foton.order.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Random;

@Slf4j
@RestController
@RequestMapping("systemFile/api")
public class SystemFileController {

    @Resource
    private SystemFileProxy systemFileProxy;

    @Resource
    private MoneySum basePriceSum;   //基础价格计算

    @Resource
    private FullMoneySum fullPriceSum;  //满减价格计算

    @Resource
    private VipPriceSum vipPriceSum;   //会员价格计算

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderMintor orderMintor;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    /**
     * 代理模式
     * @param file
     * @return
     */
    @PostMapping("/fileUpload")
    public String fileUpload(@RequestParam("file") MultipartFile file) {

        return systemFileProxy.uploadFile(file);
    }


    /**
     *
     * @param order
     * @return
     */
    @PostMapping("/orderSum")
    public Order orderSum(@RequestBody Order order) {
        fullPriceSum.setMoneySum(basePriceSum);
        vipPriceSum.setMoneySum(fullPriceSum);
        return vipPriceSum.money(order);
    }


    @PostMapping(value = "/addOrder")
    public void addOrder(OrderBase orderBase){
//        orderMapper.insertOrder(orderBase);

        //保存到延时队列，便于订单超时业务的处理

        //10-20秒取随机数
        int randCycle = new Random().nextInt(10);

        OrderDto orderDto = new OrderDto(orderBase.getOrderId(),randCycle+10L);
        orderMintor.addQueue(orderDto);

    }

}
